package org.nemanjaristic.servlets;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.nemanjaristic.utils.RDFUtils;

import com.google.code.facebookapi.schema.User;

public class TestServlet extends HttpServlet {

	private static final long serialVersionUID = -50632807968227298L;

	private static RDFUtils utils;
	
	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public TestServlet() {}

	
	@Override
	public void init() throws ServletException {
		utils = new RDFUtils();
		super.init();
	}
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		proccess(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		proccess(request, response);
	}

	private void proccess(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		utils.createRDFModel();
		List<User> users = utils.populateUsersFromDB();
		forwardTo(request, response, "WEB-INF/friends.jsp", users);
	}

	private void forwardTo(HttpServletRequest req, HttpServletResponse resp,String pagePath, Object... objects){
		RequestDispatcher dispatcher = req.getRequestDispatcher(pagePath);
		try {
			req.setAttribute("users", objects[0]);
			dispatcher.forward(req, resp);
		} catch (ServletException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
